package com.vision.sales.report.cl.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.menu.CheckMenuItem;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.vision.core.cl.ui.TabItemPanel;
import com.vision.core.cl.ui.UI;
import com.vision.core.cm.PageOrientation;
import com.vision.core.cm.PageSize;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cl.resource.SalesResources;

/**
 * 
 * @author Mark
 *
 */
public class CustomerLedgerReportPanel extends TabItemPanel {

	interface Binder extends UiBinder<Widget, CustomerLedgerReportPanel> { }

	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer mainPanel;
	
	@UiField TextButton printBtn;
	
	@UiField TextButton sizeBtn;
	@UiField Menu sizeBtnMenu;
	@UiField CheckMenuItem letterMenuItem;
	@UiField CheckMenuItem legalMenuItem;
	@UiField CheckMenuItem a4MenuItem;
	
	@UiField TextButton orientationBtn;
	@UiField Menu orientationBtnMenu;
	@UiField CheckMenuItem portraitMenuItem;
	@UiField CheckMenuItem landscapeMenuItem;

	@UiField DateField startDate;
	@UiField DateField endDate;
	@UiField TextField title;
	@UiField CheckBox showZeroes;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@SuppressWarnings("deprecation")
	public CustomerLedgerReportPanel() {
		setWidget(binder.createAndBindUi(this));
		
		letterMenuItem.setChecked(true);
		portraitMenuItem.setChecked(true);
		
		title.setValue("Customer Ledger");
		title.addValidator(Validators.TITLE_VALIDATOR);
		
		startDate.setPropertyEditor(UI.datePropertyEditor());
		endDate.setPropertyEditor(UI.datePropertyEditor());
		showZeroes.setValue(false);

		Date today = new Date();
		Date monthAgo = (Date) today.clone();
		monthAgo.setMonth(monthAgo.getMonth() - 1);
		
		startDate.setValue(monthAgo);
		endDate.setValue(today);
	}
		
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// UiHandler Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	@UiHandler("printBtn")
	void printClicked(SelectEvent e) {
		printBtn.focus();
		
		DateTimeFormat fmt = DateTimeFormat.getFormat("yyyyMMdd");
		StringBuilder params = new StringBuilder();
		params.append("orientation=" + getOrientation());
		params.append("&size=" + getSize());
		params.append("&title=" + title.getValue());
		params.append("&span=" + fmt.format(startDate.getValue()) + "-" + fmt.format(endDate.getValue()));
		params.append("&showzeroes=" + showZeroes.getValue());
		Window.open("print/customer_ledger?" + params.toString(), null, null);
	}
	
	@UiHandler("sizeBtnMenu")
	void onSizeSelection(SelectionEvent<Item> e) {
		Item item = e.getSelectedItem();
		if (item == letterMenuItem) {
			sizeBtn.setText(PageSize.LETTER.getDisplayText());
		} else if (item == legalMenuItem) {
			sizeBtn.setText(PageSize.LEGAL.getDisplayText());
		} else if (item == a4MenuItem) {
			sizeBtn.setText(PageSize.A4.getDisplayText());
		}
	}
	
	@UiHandler("orientationBtnMenu")
	void onOrientationSelection(SelectionEvent<Item> e) {
		Item item = e.getSelectedItem();
		if (item == portraitMenuItem) {
			orientationBtn.setText(PageOrientation.PORTRAIT.getDisplayText());
		} else if (item == landscapeMenuItem) {
			orientationBtn.setText(PageOrientation.LANDSCAPE.getDisplayText());
		}
	}
	
	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	@Override
	public String getPanelId() {
		return SalesTasks.CUSTOMER_LEDGER.getId();
	}

	@Override
	public TabItemConfig getConfig() {
		if (config == null) {
			config = new TabItemConfig("Customer Ledger", true);
			config.setIcon(SalesResources.INSTANCE.customerLedger());
		}
		return config;
	}


	@Override
	public void loadData() {
		
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	PageSize getSize() {
		if (a4MenuItem.isChecked())
			return PageSize.A4;
		if (legalMenuItem.isChecked())
			return PageSize.LEGAL;
		return PageSize.LETTER;
	}
	
	PageOrientation getOrientation() {
		return portraitMenuItem.isChecked() ? PageOrientation.PORTRAIT : PageOrientation.LANDSCAPE;
	}

}
